<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/1/16
 * Time: 10:45
 */

namespace app\controller\phone;
use app\model\phone\Keyword AS model;
use think\Exception;

class Index extends Base
{
    public function index(){
        $keyword = input('keyword','');
        if(!empty($keyword)){
            try {
                $keyword = str_replace('，',',',$keyword);
                $keywords = explode(',',$keyword);
                foreach ($keywords AS $key => $keyword) {
                    $find = model::where('keyword', $keyword)->find();
                    if (empty($find->keyword)) {
                        $service = new \app\service\Keyword();
                        $string = $service->save($keyword);
                        if ($string == 'NO')
                            throw new Exception('');
                        $find = model::where('keyword', $keyword)->find();
                    }
                    $array[] = $find;
                }
            }catch (\Exception $e){
                $keywords = [
                    'keyword' => $array,
                    'price' => 0
                ];
            }
            return $this->msg($array);
        }
        return $this->view('/phone/index');
    }
}